package com.greenant.store.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import com.avos.avoscloud.AVUser;
import com.greenant.store.AntHomeApplication;
import com.greenant.store.R;
import com.greenant.store.bean.StoreInfo;
import com.greenant.store.service.RetrieveDataAsyn;
import com.greenant.store.service.RetrieveDataListener;
import com.greenant.store.utils.UtilTools;

public class StartupActivity extends BaseActivity {
	private Handler mHandler = new Handler(Looper.getMainLooper());

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.start_up_activity);
		
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				if(AVUser.getCurrentUser() == null) {
					gotoLogInActivity();
				}
				else if(!UtilTools.getFlagStoreInfo(getApplication())) {
					gotoLogInActivity();
				}
				else {
					retrieveStoreInfo();
				}
			}
		}, 500);
	}
	
	private void retrieveStoreInfo() {
		RetrieveDataAsyn.getInstance().getStoreInfoByUserId(-1,
				new RetrieveDataListener() {
					@Override
					public void dataRetrieved(int requestId, Object data) {
						List<StoreInfo> storeInfoList = (List<StoreInfo>)data;
						if(storeInfoList.size() > 0) {
							AntHomeApplication.sCurrentStoreInfo = storeInfoList.get(0);
							gotoMainPage();
						}
						else{
							gotoLogInActivity();
						}
					}
				});
		
		RetrieveDataAsyn.getInstance().getEmrecvCompany(0, null);
	}
	
	private void gotoMainPage() {
		Intent mainIntent = new Intent(activity, MainActivity.class);
		startActivity(mainIntent);
		this.finish();
	}
	
	private void gotoLogInActivity() {
		Intent mainIntent = new Intent(activity, LoginActivity.class);
		startActivity(mainIntent);
		this.finish();
	}
}
